package util;

/**
 * 模仿 LeetCode 格式，定义 链表 的数据结构
 * 
 * @author Liu Huan
 */

public class ListNode {
    public int val;
    public ListNode next;
    
    public ListNode(int x){
        val = x;
    }
    
    /**
     * 由 数组 生成 链表
     * @param   array Integer[]
     * @return        ListNode
     */
    public static ListNode getListFromArray(Integer[] array){
        ListNode point = new ListNode(0);
        ListNode head = point;
        for (Integer x : array) {
            ListNode temp = new ListNode(x);
            point.next = temp;
            point = temp;
        }
        point.next = null;
        return head.next;
    }
    
    /**
     * 打印 链表里的每个值
     * @param head ListNode
     */
    public static void printList(ListNode head){
        while (head != null){
            System.out.print(head.val + " -> ");
            head = head.next;
        }
        System.out.println("null");
    }
}
